package com.hpeng.basic.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.annotation.Resource;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.basic.service.CategoryManager;
import com.hpeng.sys.model.Category;
import com.hpeng.sys.service.Function;
import com.opensymphony.xwork2.ActionSupport;

@Component("categoryImportExcelAction")
@Scope("prototype")
public class CategoryImportExcelAction extends ActionSupport{

	/**
	 * 资产类别信息表录入
	 */
	private static final long serialVersionUID = 7263610989237341369L;
	
	private File upfile;
	
	@Resource
	private CategoryManager categoryManager;
	@Resource
	private Function function;
	
	@Override
	public String execute() {
		Workbook wb = null;
		try {
			InputStream is = new FileInputStream(upfile);
			wb = Workbook.getWorkbook(is);
			Sheet sheet = wb.getSheet(0);	//获取工作簿
			String[] columnName;
			String firstLevelCatId = "0"; //初始化第一级类别Id
			String secondLevelCatId = firstLevelCatId+"001"; //初始化第二级类别Id
			for(int i=0; i<sheet.getRows(); i++){
				//获取每列单元格
				Cell[] cells = sheet.getRow(i);
				if(cells != null && cells.length > 0){
					if(i!=0){					
						//获取每个单元格内容
						String[] dataCells = new String[cells.length];
						
						for(int j=0; j<cells.length; j++){
							dataCells[j] = cells[j].getContents();
						}// columns
						
						if("".equals(dataCells[0].trim())){
							if("".equals(dataCells[1].trim())){
								//第二级分类为空   默认为第三级分类
								Category cat3 = new Category();
								cat3.setCatid(secondLevelCatId);
								cat3.setCatname(dataCells[2]);
								cat3 = categoryManager.autoAddCategory(cat3);
							}else{
								//第二级分类不为空  增加一条记录
								Category cat2 = new Category();
								cat2.setCatid(firstLevelCatId);
								cat2.setCatname(dataCells[1]);
								cat2 = categoryManager.autoAddCategory(cat2);
								secondLevelCatId = cat2.getCatid();
							}
						}else{
							//第一级分类不为空
							Category cat1 = new Category();
							cat1.setCatid("0");
							cat1.setCatname(dataCells[0]);
							cat1 = categoryManager.autoAddCategory(cat1);
							firstLevelCatId = cat1.getCatid();
						}
					}else{
						//工作簿第一列
						columnName = new String[cells.length];
						for(int j=0; j<cells.length; j++){
							columnName[j] = cells[j].getContents();
						}
					}
				}
			}//one sheet
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(wb != null){
				wb.close();
			}
		}
		return "success";
	}

	public void setUpfile(File upfile) {
		this.upfile = upfile;
	}

	public File getUpfile() {
		return upfile;
	}

	public void setCategoryManager(CategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}

	public CategoryManager getCategoryManager() {
		return categoryManager;
	}
}

//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		StringBuffer path=new StringBuffer("/WEB-INF/uploadfiles/");  
//		String year = sdf.format((new Date())).substring(0, 4);
//		String month = sdf.format((new Date())).substring(5,7);
//		String day = sdf.format((new Date())).substring(8);
//		String realPath = request.getSession().getServletContext().getRealPath(path.toString())+"/"+year
//				+"/"+month+"/"+day;  